﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Form
 * 唯一标识：23bd5158-a42c-4fe5-95cd-6cfe874fbed7
 * 文件名：Input
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/6/28 17:50:46
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// 文本输入表单控件
/// </summary>
public sealed class Input : ControlBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Input()
    {
    }

    /// <inheritdoc/>
    public override string Id { get; set; }

    /// <summary>
    /// v-model绑定的名称
    /// </summary>
    public override string Title { get; set; }

    /// <inheritdoc/>
    public override string ConverToHtml()
    {
        StringBuilder sb = new StringBuilder();

        Param par = new Param();

        if (Clearable) par.Add("clearable");

        if(InputType== InputTypeEnum.TextArea)
        {
            par.Add($":rows=\"{RowCount}\"");
        }

        sb.AppendLine($"<el-input v-model=\"{Title}\" type=\"{InputType.GetDescription()}\"  placeholder=\"{Watermark}\" {par}/>");

        return sb.ToString();
    }

    /// <summary>
    /// 水印
    /// </summary>
    internal string Watermark { get; set; }

    /// <summary>
    /// 一键清空
    /// </summary>
    internal bool Clearable { get; set; }

    /// <summary>
    /// 文本框类型
    /// </summary>
    internal InputTypeEnum InputType { get; set; }

    /// <summary>
    /// 文本域的时候行数量
    /// </summary>
    internal int RowCount { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建文本输入框
    /// </summary>
    /// <param name="vModelName"></param>
    /// <param name="watermark"></param>
    /// <param name="clearable">一键清空</param>
    /// <returns></returns>
    public static Input CreateInput(string vModelName,  string watermark = "", bool clearable = false)
    {
        Input input = new Input()
        {
            Id = GenerateId(),
            Title = vModelName,
            Watermark = watermark,
            Clearable = clearable,
            InputType =  InputTypeEnum.Text
        };

        return input;
    }

    /// <summary>
    /// 创建文本域输入框
    /// </summary>
    /// <param name="vModelName"></param>
    /// <param name="watermark"></param>
    /// <param name="clearable"></param>
    /// <returns></returns>
    public static Input CreateTextArea(string vModelName,int rowCount=2, string watermark = "", bool clearable = false)
    {
        Input input = new Input()
        {
            Id = GenerateId(),
            Title = vModelName,
            Watermark = watermark,
            Clearable = clearable,
            InputType = InputTypeEnum.TextArea,
            RowCount= rowCount
        };

        return input;
    }
}

/// <summary>
/// 表单-文本输入框类型
/// </summary>
public enum InputTypeEnum
{
    /// <summary>
    /// 默认
    /// </summary>
    [Description("text")]
    Text,
    /// <summary>
    /// 文本域
    /// </summary>
    [Description("textarea")]
    TextArea
}